﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tellstick;

namespace Alfred
{
    class Device
    {
		static List<TellstickDevice> periph;
		static ConnectionDetails connection;
		static List<TellStickLiveSensor> sensors;

		static Device()
		{
			var ck = "FEHUVEW84RAFR5SP22RABURUPHAFRUNU";
			var cks = "ZUXEVEGA9USTAZEWRETHAQUBUR69U6EF";
			var to = "e1da4e4f8f2d705c23f24032bc984c9d050423c7a";
			var tos = "c8cd435512952def2c5d5b321a2f01f0";
			connection = new ConnectionDetails(ck, cks, to, tos);

			periph = TellstickController.GetDevices();
			sensors = TellstickLiveController.GetSensors(connection);
		}
        public static List<string> GetAllDeviceListString()
        {
            List<string> list = new List<string>();
            List<TellstickDevice> devices = TellstickController.GetDevices();
            foreach (string s in devices.Select(d => d.Name))
            {
                byte[] myASCIIBytes = Encoding.Default.GetBytes(s);
                string str;
                str = Encoding.UTF8.GetString(myASCIIBytes);
                list.Add(str);
            }
            return list;
        }

        public static void Light(int state, string piece)
        {
            string str;
            byte[] myASCIIBytes = Encoding.UTF8.GetBytes(piece);
            str = Encoding.Default.GetString(myASCIIBytes);


            TellstickDevice p = periph.First(d=>d.Name == str);
            if (state == 0)
                TellstickController.TurnOff(p.ID);
            else if (state == 1)
                TellstickController.TurnOn(p.ID);
            else
                TellstickController.Dim(p.ID, state);
        }

        public static void LaunchBlink()
        {
            System.Timers.Timer timerUpdate = timerUpdate = new System.Timers.Timer(1000);
            timerUpdate.Elapsed += new System.Timers.ElapsedEventHandler(Blink);
            timerUpdate.Enabled = true;
        }

        public static void Blink(object sender, System.Timers.ElapsedEventArgs e)
        {
            Light(1, "feu rouge");
            Light(0, "feu rouge");
        }
    }
}
